﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GameBase
{
    public class VisibleGroup : VisibleGameEntity
    {
        protected List<VisibleGameEntity> listChildren = new List<VisibleGameEntity>();
        
        public override float Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                foreach (VisibleGameEntity child in listChildren)
                {
                    child.BaseScale = GetDrawScale() ;
                    child.BasePosition = GetDrawPosition();
                }
            }
        }
        public override float Rotate
        {
            get { return rotate; }
            set 
            {
                rotate = value;
                foreach (VisibleGameEntity child in listChildren)
                {
                    child.BaseRotate = GetDrawRotate();
                    child.BasePosition = GetDrawPosition();
                }
            }
        }
        public override Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
       
                foreach (VisibleGameEntity child in listChildren)
                {
                    child.BasePosition = GetDrawPosition();
                }
            }
        }
        public override Vector2 BasePosition
        {
            get { return basePosition; }
            set {
                basePosition = value;
                foreach (VisibleGameEntity child in listChildren)
                {
                    child.BasePosition = GetDrawPosition();
                }
            }
        }
        public override float BaseScale
        {
            get { return baseScale; }
            set { 
                baseScale = value;
                foreach (VisibleGameEntity child in listChildren)
                {
                    child.BaseScale = GetDrawScale();
                    child.BasePosition = GetDrawPosition();
                }
            }
        }
        public override float BaseRotate
        {
            get { return baseRotate; }
            set { 
                baseRotate = value;
                rotate = value;
                foreach (VisibleGameEntity child in listChildren)
                {
                    child.BaseRotate = GetDrawRotate();
                    child.BasePosition = GetDrawPosition();
                }
            }
        }
        public override bool Visible
        {
            get { return isVisible; }
            set 
            {
                isVisible = value;
            }
        }

        public List<VisibleGameEntity> ListChildren
        {
            get { return listChildren; }
            set { listChildren = value; }
        }

        public virtual void AddChild(VisibleGameEntity child)
        {
            child.BaseRotate = rotate + baseRotate;
            child.BaseScale = scale * baseScale;
            child.BasePosition = GetDrawPosition();
            child.Parent = this;
            ListChildren.Add(child);
        }

        public virtual void AddChilds(List<VisibleGameEntity> childs)
        {
            foreach (VisibleGameEntity child in childs)
                AddChild(child);
        }

        public virtual void RemoveChild(VisibleGameEntity child)
        {
            if (child.Parent == this)
            {
                child.Parent = null;
            }
            ListChildren.Remove(child);
        }

        /*public virtual void RemoveChild(int i)
        {
            ListChildren.RemoveAt(i);
        }*/

        public virtual void RemoveAllChild()
        {
            ListChildren.Clear();
        }

        public VisibleGameEntity GetChild(int i)
        {
            return ListChildren[i];
        }

        public int CountChild()
        {
            return ListChildren.Count;
        }

        public override void Update(GameTime gameTime)
        {
            //foreach(VisibleGameEntity child in listChildren)
            //{
            //    child.Update(gameTime);
            //}
            for (int i = 0; i < listChildren.Count; i++)
                listChildren[i].Update(gameTime);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (!isVisible)
            {
                return;
            }
            /*foreach (VisibleGameEntity child in listChildren)
            {
                if(child.Visible)
                    child.Draw(gameTime, spriteBatch);
            }*/
            for (int i = 0; i < listChildren.Count; i++)
            {
                VisibleGameEntity child = listChildren[i];
                if (child.Visible)
                    child.Draw(gameTime, spriteBatch);
            }
        }
        
    }
}
